/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPhieuThue.java
 *
 * Created on Dec 4, 2011, 9:19:54 AM
 */
package uit.qlks.from;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.WebRowSet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import uit.ejb.dto.HoaDonDTO;
import uit.qlks.baocao.BaoCao;
import uit.qlks.controller.hoadon.HoaDonController;
import uit.qlks.controller.khachhang.KhachHangController;
import uit.qlks.controller.phieuthue.PhieuThueController;
import uit.qlks.controller.phieuthuedv.PhieuThueDVController;
import uit.qlks.controller.phong.PhongController;

/**
 *
 * @author QUANGTHIEU
 */
public class frmPhieuThue extends javax.swing.JFrame {
//public DefaultTableModel model;
    private String[] tableColumnsName = {"STT","Mã PT ","Mã Phòng","Tên Phòng","Mã KH","Tên Khách Hàng", "Ngày Thuê", "Ngày Trả"};
    /** Creates new form frmPhieuThue */
    public static frmPhieuThue _frm;
    public frmPhieuThue() {
        initComponents();
        
        cmbthang.setSelectedIndex(-1);
        try
        {
            WebRowSet wr = PhieuThueController.phieuThue_GetByAll();
            UpdateTable(jtdanhsach, tableColumnsName, wr);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtdanhsach = new javax.swing.JTable();
        btthem = new javax.swing.JButton();
        btxoa = new javax.swing.JButton();
        btsua = new javax.swing.JButton();
        jblaphoadon = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jcthang = new javax.swing.JCheckBox();
        cmbthang = new javax.swing.JComboBox();
        jcnam = new javax.swing.JCheckBox();
        jYnam = new com.toedter.calendar.JYearChooser();
        jdngaythue = new com.toedter.calendar.JDateChooser();
        jcngaythue = new javax.swing.JCheckBox();
        jcngaydi = new javax.swing.JCheckBox();
        jdngaydi = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        cmbtinhtrang = new javax.swing.JComboBox();
        jcpt = new javax.swing.JCheckBox();
        jblammoi = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        btnPrint = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Phiếu Thuê Phòng");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setText("Phiếu Thuê Phòng");

        jtdanhsach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "STT", "MaPhong", "TenPhong", "MaKH", "TenKH", "NgayThue", "NgayDi", "MaPT"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtdanhsach.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jtdanhsach);

        btthem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add.png"))); // NOI18N
        btthem.setText("Thêm");
        btthem.setBorder(null);
        btthem.setContentAreaFilled(false);
        btthem.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add2.png"))); // NOI18N
        btthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btthemActionPerformed(evt);
            }
        });

        btxoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete.png"))); // NOI18N
        btxoa.setText("Xóa");
        btxoa.setBorder(null);
        btxoa.setContentAreaFilled(false);
        btxoa.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete2.png"))); // NOI18N
        btxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btxoaActionPerformed(evt);
            }
        });

        btsua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit.png"))); // NOI18N
        btsua.setText("Sửa");
        btsua.setBorder(null);
        btsua.setContentAreaFilled(false);
        btsua.setMaximumSize(new java.awt.Dimension(51, 21));
        btsua.setMinimumSize(new java.awt.Dimension(51, 21));
        btsua.setPreferredSize(new java.awt.Dimension(51, 21));
        btsua.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit2.png"))); // NOI18N
        btsua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btsuaActionPerformed(evt);
            }
        });

        jblaphoadon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/bill.png"))); // NOI18N
        jblaphoadon.setText("Lập Hóa Đơn");
        jblaphoadon.setBorder(null);
        jblaphoadon.setContentAreaFilled(false);
        jblaphoadon.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/bill2.png"))); // NOI18N
        jblaphoadon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jblaphoadonActionPerformed(evt);
            }
        });

        jcthang.setName("jchthang"); // NOI18N
        jcthang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcthangActionPerformed(evt);
            }
        });

        cmbthang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Một", "Hai", "Ba", "Bốn", "Năm", "Sáu", "Bảy", "Tám", "Chín", "Mười", "Mười Một", "Mười Hai" }));
        cmbthang.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbthangItemStateChanged(evt);
            }
        });

        jcnam.setName("jchthang"); // NOI18N
        jcnam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcthangActionPerformed(evt);
            }
        });

        jdngaythue.setDateFormatString("dd/M/yyyy");

        jcngaythue.setName("jchthang"); // NOI18N
        jcngaythue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcngaythueActionPerformed(evt);
            }
        });

        jcngaydi.setName("jchthang"); // NOI18N
        jcngaydi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcngaydiActionPerformed(evt);
            }
        });

        jdngaydi.setDateFormatString("dd/M/yyyy");

        jLabel2.setText("Ngày Thuê:");

        jLabel3.setText("Ngày Đi:");

        jLabel4.setText("Tháng:");

        jLabel5.setText("Năm:");

        jLabel7.setText("PT:");

        cmbtinhtrang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Đã Thanh Toán", "Chưa Thanh Toán" }));
        cmbtinhtrang.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbtinhtrangItemStateChanged(evt);
            }
        });

        jcpt.setName("jchthang"); // NOI18N
        jcpt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcptjcthangActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jcngaythue)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jcngaydi)
                                .addGap(6, 6, 6)
                                .addComponent(jLabel3))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jcthang)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcnam)
                                    .addComponent(jcpt))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5))))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbtinhtrang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jYnam, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbthang, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jdngaydi, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jdngaythue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcngaythue, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jdngaythue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcngaydi)
                    .addComponent(jdngaydi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcthang)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmbthang, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcnam, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcpt, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                                .addComponent(cmbtinhtrang, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jYnam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jblammoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/reload.png"))); // NOI18N
        jblammoi.setText("Lấy Danh Sách");
        jblammoi.setBorder(null);
        jblammoi.setContentAreaFilled(false);
        jblammoi.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/reload2.png"))); // NOI18N
        jblammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jblammoiActionPerformed(evt);
            }
        });

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/product-sales.png"))); // NOI18N

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Printer.png"))); // NOI18N
        btnPrint.setText("Print");
        btnPrint.setBorder(null);
        btnPrint.setContentAreaFilled(false);
        btnPrint.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Printer2.png"))); // NOI18N
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel1)
                        .addGap(252, 252, 252))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(jblammoi)
                .addGap(194, 194, 194)
                .addComponent(btthem)
                .addGap(73, 73, 73)
                .addComponent(btxoa)
                .addGap(74, 74, 74)
                .addComponent(btsua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63)
                .addComponent(jblaphoadon)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                .addComponent(btnPrint)
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jblammoi)
                    .addComponent(btthem)
                    .addComponent(btxoa)
                    .addComponent(btsua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jblaphoadon)
                        .addComponent(btnPrint))))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1000)/2, (screenSize.height-471)/2, 1000, 471);
    }// </editor-fold>//GEN-END:initComponents

    private int between2Date(Calendar ngaysau,Calendar ngaytruoc)
    {
        return (int) ((ngaysau.getTime().getTime() - ngaytruoc.getTime().getTime())/(24*3600*1000));
        
    }
    public int getCountDay(int y1, int m1,int d1,int y2, int m2,int d2)
    {
        //y1 - y2
            Calendar ngaythue = Calendar.getInstance();
            Calendar ngaydi = Calendar.getInstance();
            Calendar temp = Calendar.getInstance();
            int namthue;
           // namdi = y1;
            namthue = y2;
            ngaythue.set(y2,m2,d2);
            ngaydi.set(y1,m1,d1);
            temp.set(y1,m1,d1);
           
            int days = 0;
            if(y1 > namthue)
            {
                ngaydi.set(namthue,12,31);
                days = between2Date(ngaydi, ngaythue);
                namthue++;
                if(y1 > namthue)
                {
                    days += 365*(y1 - namthue);
                }
                ngaythue.set(y1,1,1);
                days += between2Date(temp, ngaythue)+2;
            }
            else
            {
                days = between2Date(ngaydi, ngaythue) + 1;
            }
//            JOptionPane.showConfirmDialog(rootPane, days);
            return days;
    }
private void btthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btthemActionPerformed

    try
    {
        frmDatPhong frdp =new frmDatPhong();
        frdp.preframe = 0;
        frdp.setVisible(true);
        frdp.setFocusable(true);
        _frm = this;
        this.setVisible(false);

    }
    catch(Exception e){}

}//GEN-LAST:event_btthemActionPerformed

private void btxoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btxoaActionPerformed

                   
    try
    {
        int rowindex = jtdanhsach.getSelectedRow();
        if(rowindex == -1)
        {
              JOptionPane.showMessageDialog(rootPane, "Chưa chọn phiếu để xóa","Warning", JOptionPane.WARNING_MESSAGE);
              return;
        }
            
        if(  JOptionPane.showConfirmDialog(null,  "Bạn có muốn hủy phiếu thuê không? Tất cả dữ liệu liên quan sẽ mất hêt."
                        , "Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
        {
            //xoa phieu dich vu
            PhieuThueDVController.phieuThueDV_Delete(jtdanhsach.getValueAt(rowindex, 1).toString().substring(2));
            //xoa hoa don            
            WebRowSet wrs = HoaDonController.hoaDon_GetByTop("", " MaPhieuThue = '"+jtdanhsach.getValueAt(rowindex, 1).toString().substring(2)+"' ", "");
            if(wrs.next())
                HoaDonController.hoaDon_Delete(wrs.getString("MaHD"));
            //Xoa phieu dat phong
            PhieuThueController.phieuThue_Delete(jtdanhsach.getValueAt(rowindex, 1).toString().substring(2));
            //Xoa khach hang
            KhachHangController.khachHang_Delete(jtdanhsach.getValueAt(rowindex, 3).toString().substring(2));
            //cap nhat lai trang thai phong
            ResultSet rs =  PhieuThueController.phieuThue_GetByTop("", " MaPhong = '"+ jtdanhsach.getValueAt(rowindex, 2).toString().substring(2) +"' ", "");
            if(rs != null)
            {
               try {
                       rs.next();
                        if(rs.getRow() == 0)//Neu phong dat truoc do khong co ai thue thi set lai phong trong
                        {
                            PhongController.phong_UpdateStatus(jtdanhsach.getValueAt(rowindex, 2).toString().substring(2), "1");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(frmDatPhong.class.getName()).log(Level.SEVERE, null, ex);
                    }
             }
           //reset du lieu
                WebRowSet wr = PhieuThueController.phieuThue_GetByAll();
                UpdateTable(jtdanhsach, tableColumnsName, wr);
            
            JOptionPane.showMessageDialog(rootPane, "Hủy thành công!");
        }
    }
    catch(Exception e)
    {
         JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_btxoaActionPerformed

private void UpdateTable(JTable jt, String[] tableColumnsName, WebRowSet wrs){
        try{
            DefaultTableModel dfTable = (DefaultTableModel) jt.getModel();
            dfTable.getDataVector().removeAllElements();
            dfTable.setColumnIdentifiers(tableColumnsName);
           // ResultSetMetaData rsmd = wrs.getMetaData();
            Vector rowData;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
            while(wrs.next())
            {
                rowData = new Vector() ;
                rowData.add (wrs.getRow());
                rowData.add ("PT"+wrs.getString("MaPhieuThue"));
                rowData.add ("MP"+wrs.getString("MaPhong"));
                WebRowSet p = PhongController.phong_GetById(wrs.getString("MaPhong"));
                while(p.next()){
                    rowData.add(p.getString("TenPhong"));
                }
                rowData.add ("KH"+wrs.getString("MaKH"));
                p = KhachHangController.khachHang_GetById(wrs.getString("MaKH"));
                while(p.next()){
                    rowData.add(p.getString("TenKH"));
                }
                Date d = wrs.getDate("NgayThue");
                rowData.add (sdf.format(d));
                d = wrs.getDate("NgayDi");
                rowData.add (sdf.format(d));
 
                dfTable.addRow(rowData);
            }
            jt.setModel(dfTable);
            jtdanhsach.getColumnModel().getColumn(0).setPreferredWidth(10);
            jtdanhsach.getColumnModel().getColumn(1).setPreferredWidth(30);
            jtdanhsach.getColumnModel().getColumn(2).setPreferredWidth(40);
            jtdanhsach.getColumnModel().getColumn(3).setPreferredWidth(40);
            jtdanhsach.getColumnModel().getColumn(4).setPreferredWidth(30);
            jtdanhsach.getColumnModel().getColumn(5).setPreferredWidth(150);
            jtdanhsach.getColumnModel().getColumn(6).setPreferredWidth(50);
            jtdanhsach.getColumnModel().getColumn(7).setPreferredWidth(50);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
private void cmbthangItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbthangItemStateChanged

        if(jcthang.isSelected())
            getselectwhere();

}//GEN-LAST:event_cmbthangItemStateChanged

public void getselectwhere()
{
    
    try
     {
         String where = "";
         if(jcngaythue.isSelected())
         {
             if("null".equals(jdngaythue.getDate() + ""))
             {
                 JOptionPane.showMessageDialog(rootPane, "Chưa chọn ngày thuê","Warning", JOptionPane.WARNING_MESSAGE);
                 return;
             }
                 
            String date = (jdngaythue.getDate().getMonth() + 1 )+"/"+jdngaythue.getDate().getDate()+ "/" + (jdngaythue.getDate().getYear() + 1900);
            where = " NgayThue = '"+date+"' ";
            
         }
         if(jcngaydi.isSelected())
         {
             if("null".equals(jdngaydi.getDate() + ""))
             {
                 JOptionPane.showMessageDialog(rootPane, "Chưa chọn ngày đi","Warning", JOptionPane.WARNING_MESSAGE);
                 return;
             }
                 
            String date = (jdngaydi.getDate().getMonth() + 1 )+"/"+jdngaydi.getDate().getDate()+ "/" + (jdngaydi.getDate().getYear() + 1900);
            where = " NgayDi = '"+date+"' ";
         }
         else
         {
             if(jcthang.isSelected())
                 where = " Month(NgayThue) = '" +(cmbthang.getSelectedIndex() + 1 )+"' ";
             if(jcnam.isSelected())
                 if(jcthang.isSelected())
                     where += " and Year(NgayThue) = '"+jYnam.getYear()+"' ";
                else
                     where += " Year(NgayThue) = '"+jYnam.getYear()+"' ";
             if(jcpt.isSelected())
             {
                 if(jcnam.isSelected() || jcthang.isSelected())
                     where += " and ";
                 if(cmbtinhtrang.getSelectedIndex() == 0)
                    where += " MaPhieuThue in (Select MaPhieuThue From HoaDon ) ";
                 else
                     where += " MaPhieuThue not in (Select MaPhieuThue From HoaDon ) ";
             }
         }
        WebRowSet wr = PhieuThueController.phieuThue_GetByTop("",where , "");
        UpdateTable(jtdanhsach, tableColumnsName, wr);
     }
     catch(Exception e)
     {
          JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
     }
}

private void btsuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btsuaActionPerformed
  try
  {
       int rowindex = jtdanhsach.getSelectedRow();
       if(rowindex == -1)
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa chọn phiếu để sửa","Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }
        WebRowSet wrs = HoaDonController.hoaDon_GetByTop("", " MaPhieuThue = '"+jtdanhsach.getValueAt(rowindex, 1).toString().substring(2)+"' ", "");
        HoaDonDTO hd = new HoaDonDTO();
        hd.setMaHD("");
        if(wrs.next())//hóa đơn đa được lập
        {
            if(JOptionPane.showConfirmDialog(this, "Phiếu thuê đã được thanh toán. Bạn có muốn xem hóa đơn không?", "Question",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
            {
                hd.setMaHD(wrs.getString("MaHD"));
                hd.setTienDV(wrs.getString("TienDV").replace(".0000", ""));
                hd.setTongTien(wrs.getString("TongTien").replace(".0000", ""));
                frmHoaDon frmhd = new frmHoaDon();
                frmhd.MaKH = jtdanhsach.getValueAt(rowindex, 3).toString().substring(2);
                frmhd.MaPhieuThue = jtdanhsach.getValueAt(rowindex, 1).toString().substring(2);
                frmhd.MaPhong= jtdanhsach.getValueAt(rowindex, 2).toString().substring(2);
                frmhd.HD = hd;
                try
                {
                    //SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
                    DateFormat df = new SimpleDateFormat("dd-M-yyyy");//(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 4));
                    Date d1 = df.parse(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 5).toString());
                    Date d2 = df.parse(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 4).toString());
                    frmhd.SoNgay = getCountDay(d1.getYear() + 1900, d1.getMonth() + 1, d1.getDate(), d2.getYear() + 1900, d2.getMonth() + 1, d2.getDate());
                }
                catch(Exception e){ frmhd.SoNgay = 0;}
                frmhd.setVisible(true);  
                frmhd.setFocusable(true);
                _frm = this;
                this.setVisible(false);
                return;
            }
            else return;//không muốn cập nhật
        }
        else//cho phep sua
        {
            frmDatPhong frmdp = new frmDatPhong();
            frmdp.MaKH = jtdanhsach.getValueAt(rowindex, 3).toString().substring(2);
            frmdp.MaPT = jtdanhsach.getValueAt(rowindex, 1).toString().substring(2);
            frmdp.MP = jtdanhsach.getValueAt(rowindex, 2).toString().substring(2);
            frmdp.preframe = 0;
            frmdp.set_phieudatphong();
            frmdp.setVisible(true);
            frmdp.setFocusable(true);
            _frm = this;
            this.setVisible(false);
        }
  }
  catch(Exception e)
  {
        JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
  }
    //JOptionPane.showConfirmDialog(rootPane, frmdp.MaKH + frmdp.MaPT);
    // TODO add your handling code here:
}//GEN-LAST:event_btsuaActionPerformed

private void jcthangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcthangActionPerformed

    jcngaythue.setSelected(false);
    jcngaydi.setSelected(false);
    // TODO add your handling code here:
}//GEN-LAST:event_jcthangActionPerformed

private void jcngaythueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcngaythueActionPerformed
    jcnam.setSelected(false);
    jcthang.setSelected(false);
    jcngaydi.setSelected(false);
    jcpt.setSelected(false);
    // TODO add your handling code here:
}//GEN-LAST:event_jcngaythueActionPerformed

private void jblammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jblammoiActionPerformed

    getselectwhere();
    // TODO add your handling code here:
}//GEN-LAST:event_jblammoiActionPerformed

private void jcngaydiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcngaydiActionPerformed
    jcnam.setSelected(false);
    jcthang.setSelected(false);
    jcngaythue.setSelected(false);
    jcpt.setSelected(false);
    // TODO add your handling code here:
}//GEN-LAST:event_jcngaydiActionPerformed

private void jblaphoadonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jblaphoadonActionPerformed
    int rowindex = jtdanhsach.getSelectedRow();
    if(rowindex == -1)
    {
        JOptionPane.showMessageDialog(rootPane, "Chưa chọn phiếu để lập hóa đơn","Warning", JOptionPane.WARNING_MESSAGE);
        return;
    }
    try{
        WebRowSet wrs = HoaDonController.hoaDon_GetByTop("", " MaPhieuThue = '"+jtdanhsach.getValueAt(rowindex, 1).toString().substring(2)+"' ", "");
        HoaDonDTO hd = new HoaDonDTO();
        hd.setMaHD("");
        if(wrs.next())//hóa đơn đx được lập
        {
            if(JOptionPane.showConfirmDialog(this, "Hóa đơn đã tồn tại. Bạn có muốn cập nhật lại không ?", "Question",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
            {
                hd.setMaHD(wrs.getString("MaHD"));
                hd.setTienDV(wrs.getString("TienDV").replace(".0000", ""));
                hd.setTongTien(wrs.getString("TongTien").replace(".0000", ""));
            }
            else return;//không muốn cập nhật
        }
        
        frmHoaDon frmhd = new frmHoaDon();
        frmhd.MaKH = jtdanhsach.getValueAt(rowindex, 3).toString().substring(2);
        frmhd.MaPhieuThue = jtdanhsach.getValueAt(rowindex, 1).toString().substring(2);
        frmhd.MaPhong= jtdanhsach.getValueAt(rowindex, 2).toString().substring(2);
        frmhd.HD = hd;
        frmhd.preframe = 0;
        try
        {
            //SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
            DateFormat df = new SimpleDateFormat("dd-M-yyyy");//(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 4));
            Date d1 = df.parse(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 5).toString());
            Date d2 = df.parse(jtdanhsach.getValueAt(jtdanhsach.getSelectedRow(), 4).toString());
            frmhd.SoNgay = getCountDay(d1.getYear() + 1900, d1.getMonth() + 1, d1.getDate(), d2.getYear() + 1900, d2.getMonth() + 1, d2.getDate());
        }
        catch(Exception e){ frmhd.SoNgay = 0;}
        frmhd.setVisible(true);  
        frmhd.setFocusable(true);
        _frm = this;
        this.setVisible(false);

    }
    catch(Exception e)
    {
         JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
    }
    
    // TODO add your handling code here:
}//GEN-LAST:event_jblaphoadonActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    frmMain._main.setVisible(true);
    frmMain._main.setFocusable(true);
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowClosed

private void cmbtinhtrangItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbtinhtrangItemStateChanged
// TODO add your handling code here:
}//GEN-LAST:event_cmbtinhtrangItemStateChanged

private void jcptjcthangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcptjcthangActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jcptjcthangActionPerformed

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed

    try {
            // TODO add your handling code here:
            String html1 = "";//<meta http-equiv=Content-Type content='text/html; charset=utf8'>";
            //html1 += "<LEFT>CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM</BR>----------Ðộc lập - Tự do - Hạnh phúc-----------</LEFT>";
            html1 += "<CENTER><h2>DANH SÁCH PHIẾU THUÊ</h2>"; 
//            html1 += "&nbsp;&nbsp;&nbsp<b>Năm : </b>" + ycNam.getYear();

            String footer ="";
//            footer += "<b>Tiền phòng : </b>" + lbTienPhong.getText() + " VNĐ<br>";
//            footer += "&nbsp;&nbsp;&nbsp<b>Tiền dịch vụ : </b>" + lbTienDV.getText() + " VNĐ<br>";
//            footer += "&nbsp;&nbsp;&nbsp<b>Tổng doanh thu : </b>" + lbDoanhThu.getText() + " VNĐ<br>";
            BaoCao bc = new BaoCao();
            bc.baocao(jtdanhsach,html1,footer,"400");
        } catch (Exception ex) {
//            Logger.getLogger(frmBaoCao.class.getName()).log(Level.SEVERE, null, ex);
        }

    // TODO add your handling code here:
}//GEN-LAST:event_btnPrintActionPerformed

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
//        try
//            {
//            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//set giao dien
//            }
//            catch(Exception ex){}
    // TODO add your handling code here:
}//GEN-LAST:event_formComponentShown

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmPhieuThue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmPhieuThue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmPhieuThue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmPhieuThue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmPhieuThue().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btsua;
    private javax.swing.JButton btthem;
    private javax.swing.JButton btxoa;
    private javax.swing.JComboBox cmbthang;
    private javax.swing.JComboBox cmbtinhtrang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private com.toedter.calendar.JYearChooser jYnam;
    private javax.swing.JButton jblammoi;
    private javax.swing.JButton jblaphoadon;
    private javax.swing.JCheckBox jcnam;
    private javax.swing.JCheckBox jcngaydi;
    private javax.swing.JCheckBox jcngaythue;
    private javax.swing.JCheckBox jcpt;
    private javax.swing.JCheckBox jcthang;
    private com.toedter.calendar.JDateChooser jdngaydi;
    private com.toedter.calendar.JDateChooser jdngaythue;
    private javax.swing.JTable jtdanhsach;
    // End of variables declaration//GEN-END:variables
}
